home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Tool Chest / Testing & Debugging / Debuggers & dcmds / MacsBug 6.5d6 / dcmds / C Samples / File.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-17  |  5.7 KB  |  262 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        File.c
  3.  
  4.     Contains:    This is the FCB dcmd.
  5.  
  6.     Written by:    Scott Douglas, Dave Lyons.
  7.  
  8.     Copyright:    © 1988,1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     9/13/93    DAL        made some columns wider and fixed the filename-prefix feature to
  13.                                     work again
  14.  
  15.     Modification history:
  16.         29Nov88 sad        revised for new dcmd names.
  17.          5Oct88    sad        broke out formatting routines to put.c
  18.         30Sep88 sad        written
  19.  
  20.     The following MPW commands will build the dcmd and copy it to the
  21.     "Debugger Prefs" file in the System folder. The dcmd's name in
  22.     MacsBug will be the name of the file built by the Linker.
  23.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  24.     C Samples folder into this folder.
  25.  
  26.     C Put.c
  27.     C File.c
  28.     Link dcmdGlue.a.o File.c.o put.c.o DRuntime.o "{Libraries}"Interface.o -o File
  29.     BuildDcmd File 1003
  30.     Echo 'include "File";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  31. */
  32.  
  33. #include <Types.h>
  34. #include <Memory.h>
  35. #include <Files.h>
  36. #include <OSUtils.h>
  37.  
  38. #include "dcmd.h"
  39. #include "put.h"
  40.  
  41. #define FSFCBLen 0x3f6
  42. #define FCBsPtr 0x34e
  43.  
  44. typedef struct FCB
  45. {
  46.     unsigned long fcbFlNum;
  47.     unsigned char fcbMdRByt;
  48.     unsigned char fcbTypByt;
  49.     unsigned short fcbSBlk;
  50.     unsigned long fcbEOF;
  51.     unsigned long fcbPLen;
  52.     unsigned long fcbCrPs;
  53.     VCB* fcbVPtr;
  54.     void* fcbBfAdr;
  55.     unsigned short fcbFlPos;
  56.     unsigned long fcbClmpSize;
  57.     void* fcbBTCBPtr;
  58.     unsigned long fcbExtRec[3];
  59.     OSType fcbFType;
  60.     unsigned long fcbCatPos;
  61.     unsigned long fcbDirID;
  62.     char fcbCName[32];
  63. } FCB;
  64.  
  65.  
  66. static void DrawHdr()
  67. {
  68. //                             1         2         3         4         5         6         7
  69. //                    1234567890123456789012345678901234567890123456789012345678901234567890
  70.     dcmdDrawLine("\pfRef File                   Vol         Type Fl Fork     LEof     Mark  FlNum Parent FCB at");
  71. }
  72.  
  73.  
  74. static void DrawFCB(int fref, FCB* fcbp)
  75. {
  76.     PutUHexWord(fref);
  77.     PutSpace();
  78.     PutPStrTruncTo(fcbp->fcbCName,17+10);
  79.     PutSpace();
  80.     PutPStrTruncTo(fcbp->fcbVPtr->vcbVN,26+10+3);
  81.     PutSpace();
  82.     PutOSType(fcbp->fcbFType);
  83.     PutSpace();
  84.     PutChar((fcbp->fcbMdRByt & 0x80) ? 'D' : 'd');
  85.     PutChar((fcbp->fcbMdRByt & 0x01) ? 'W' : 'w');
  86.     PutSpace();
  87.     if (fcbp->fcbMdRByt & 0x02)
  88.         PutPStr("\prsrc ");
  89.     else
  90.         PutPStr("\pdata ");
  91.     PutUDecTo(fcbp->fcbEOF,47+11+3);
  92.     PutSpace();
  93.     PutUDecTo(fcbp->fcbCrPs,55+12+3);
  94.     PutSpace();
  95.     PutUHexZTo(fcbp->fcbFlNum,6,62+12+3);
  96.     PutSpace();
  97.     PutUHexZTo(fcbp->fcbDirID,6,69+12+3);
  98.     PutSpace();
  99.     PutUHexZTo((unsigned long)fcbp,6,76+12+3);
  100.     PutLine();
  101. }
  102.  
  103.  
  104. static Boolean PrefixPStr(const Str255 astr, const Str255 bstr)
  105. // returns true if astr is equal to a prefix of bstr
  106. //    astr must not be longer than 31 characters
  107. {
  108.     char newstr[31];
  109.     int alen = *astr;
  110.     int blen = *bstr;
  111.     if (alen <= blen)
  112.       {
  113.         BlockMove(bstr+1,newstr+1,alen);
  114.         newstr[0] = alen;
  115.         return EqualString(astr,newstr,false,true);
  116.       }
  117.     else
  118.         return false;    
  119. }
  120.  
  121.  
  122.  
  123. pascal void CommandEntry(dcmdBlock* paramPtr)
  124. {
  125.     switch (paramPtr->request)
  126.       {
  127.         case dcmdInit:
  128.             break;
  129.  
  130.         case dcmdHelp:
  131.             dcmdDrawLine("\pfile [fRefNum|\"file name\"]");
  132.             dcmdDrawLine("\p   Displays file information on all open files, or for the given fRefNum or filename.");
  133.             dcmdDrawLine("\p      Flags are D/d=Dirty, W/w=writeable.");
  134.             break;
  135.  
  136.         case dcmdDoIt:
  137.           {
  138.             Boolean doOneFCB = false;
  139.             long fref;
  140.             short c;
  141.             Boolean haveFileName = false;
  142.             Str255 filename;
  143.             int fcblen;                // the length of one fcb
  144.             char* fcbsbase;
  145.             int fcbslen;            // the length of the fcbs block
  146.  
  147.             dcmdSwapWorlds();
  148.  
  149.             dcmdDrawLine("\pDisplaying File Control Blocks");
  150.  
  151.             // get low-memory values after dcmdSwapWorlds()
  152.             fcblen = *(unsigned short*)FSFCBLen;
  153.             fcbsbase = *(char**)FCBsPtr;
  154.             fcbslen = *(unsigned short*)fcbsbase;
  155.  
  156.             if (fcblen != sizeof(FCB))
  157.               {
  158.                 PutPStr("\FSFCBLen = ");
  159.                 PutUDec(fcblen);
  160.                 PutPStr("\p expected ");
  161.                 PutUDec(fcblen);
  162.                 PutLine(sizeof(FCB));
  163.               }
  164.             if ((fcbslen - 2) % fcblen != 0)
  165.               {
  166.                 PutPStr("\pBad fcbslen ");
  167.                 PutUHexWord(fcbslen);
  168.                 PutLine();
  169.               }
  170.  
  171.             c = dcmdPeekAtNextChar();
  172.             if (c == '"' || c == '\'')
  173.               {
  174.                 haveFileName = true;
  175.                 (void)dcmdGetNextParameter(filename);
  176.               }
  177.             else
  178.                 (void) dcmdGetNextExpression(&fref, &doOneFCB);
  179.  
  180.             if (doOneFCB)
  181.               {
  182.                 fref = (unsigned short)fref;
  183.                 if ((fref > fcbslen) || ((fref - 2) % fcblen != 0))
  184.                   {
  185.                     PutPStr("\pBad file refnum ");
  186.                     PutUHexWord(fref);
  187.                     PutLine();
  188.                   }
  189.                 else
  190.                   {
  191.                     FCB* fcbp = (FCB*)(fref + fcbsbase);
  192.                     if (fcbp->fcbFlNum)
  193.                       {
  194.                         DrawHdr();
  195.                         DrawFCB(fref,fcbp);
  196.                       }
  197.                     else
  198.                       {
  199.                         PutPStr("\pFCB ");
  200.                         PutUHexWord(fref);
  201.                         PutPStr("\p is not in use");
  202.                         PutLine();
  203.                       }
  204.                   }
  205.               }
  206.             else
  207.               {
  208.                 int numfcbs = (fcbslen - 2) / fcblen;
  209.                 int fcbsused = 0;
  210.                 Boolean foundOne = false;
  211.                 for (fref = 2; fref < fcbslen; fref += fcblen)
  212.                   {
  213.                     FCB* fcbp = (FCB*)(fcbsbase + fref);
  214.                     if (fcbp->fcbFlNum &&
  215.                         (!haveFileName || PrefixPStr(filename,fcbp->fcbCName)))
  216.                       {
  217.                         fcbsused++;
  218.                         if(!foundOne)
  219.                           {
  220.                             DrawHdr();
  221.                             foundOne = true;
  222.                           }
  223.                         DrawFCB(fref,fcbp);
  224.                       }
  225.                     if (paramPtr->aborted)
  226.                         break;
  227.                   }
  228.                 if(!paramPtr->aborted)
  229.                     if (haveFileName)
  230.                       {
  231.                         if (!foundOne)
  232.                           {
  233.                             PutPStr("\pNo open files match \"");
  234.                             PutPStr(filename);
  235.                             PutChar('"');
  236.                             PutLine();
  237.                           }
  238.                       }
  239.                     else
  240.                       {
  241.                         PutUDec(numfcbs);
  242.                         PutPStr("\p FCBs, ");
  243.                         PutUDec(fcbsused);
  244.                         PutPStr("\p in use, ");
  245.                         PutUDec(numfcbs - fcbsused);
  246.                         PutPStr("\p free");
  247.                         PutLine();
  248.                       }
  249.               }    
  250.  
  251.             dcmdSwapWorlds();
  252.             break;
  253.           }
  254.  
  255.         default:
  256.             PutPStr("\punknown request ");
  257.             PutUDec(paramPtr->request);
  258.             PutLine();
  259.             break;
  260.       }
  261. }
  262.